package org.jeecg.modules.login.handler.wechat;

import lombok.Data;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.crypto.SHA1;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * 微信公众号执行器
 *
 * @author tianyi.jiang
 * @since 1.0.0
 */
@Slf4j
@Component
@ConditionalOnExpression("'${basicplatform.wechat.public.enable:false}'.equals('true')")
public class WeChatPublicHandler {

    private final WeChatPublicProperty weChatPublicProperty;

    /**
     * 二维码地址
     *
     * @param sceneId 场景Id
     * @return QR Code url
     */
    @SneakyThrows(value = WxErrorException.class)
    public String getQr(String sceneId) {
        WxMpQrCodeTicket wxMpQrCodeTicket = weChatPublicProperty.getWxMpService().getQrcodeService().qrCodeCreateTmpTicket(sceneId, 300);
        return "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + wxMpQrCodeTicket.getTicket();
    }

    public boolean check(String signature, String timestamp, String nonce) {
        // SHA1 加密并对比
        return SHA1.gen(weChatPublicProperty.getToken(), timestamp, nonce).equals(signature);
    }

    @SneakyThrows(value = WxErrorException.class)
    public WxOAuth2UserInfo weChatUser(String code) {
        WxOAuth2AccessToken wxMpOauth2AccessToken = weChatPublicProperty.getWxMpService().getOAuth2Service().getAccessToken(code);
        log.info(wxMpOauth2AccessToken.toString());
        return weChatPublicProperty.getWxMpService().getOAuth2Service().getUserInfo(wxMpOauth2AccessToken, null);
    }

    public WeChatPublicHandler(WeChatPublicProperty weChatPublicProperty) {
        this.weChatPublicProperty = weChatPublicProperty;
    }

    @Data
    @Configuration
    @ConfigurationProperties(prefix = "basicplatform.wechat.public")
    public static class WeChatPublicProperty {
        /**
         * 应用Id
         */
        private String appId;
        /**
         * 应用秘钥
         */
        private String secret;
        /**
         * 公众号token
         */
        private String token;

        public WxMpService getWxMpService() {
            WxMpDefaultConfigImpl wxMaConfig = new WxMpDefaultConfigImpl();
            wxMaConfig.setAppId(this.getAppId());
            wxMaConfig.setSecret(this.getSecret());
            WxMpService wxMpService = new WxMpServiceImpl();
            wxMpService.setWxMpConfigStorage(wxMaConfig);
            return wxMpService;
        }
    }
}